package com.dense.kuiniu.shelfcard.card.idcard;

import android.content.Context;

import com.synjones.idcard.IDCardReaderRetInfo;
import com.synjones.reader.IDCardReaderModule;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * @author Administrator
 * @date 2020/8/31
 */
public class CardReader {
    public IDCardReaderModule idCardReaderModule;
    private volatile boolean reading = false;

    public CardReader(Context context) {
        this.idCardReaderModule = IDCardReaderModule.getInstance(context);
    }


    public void readIdCardInfo(@NonNull IDCardReadListener listener) {
        reading = true;
        Observable.create(new ObservableOnSubscribe<IDCardInfo>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<IDCardInfo> emitter) throws Throwable {
                try {
                    if (!idCardReaderModule.isOpen()) {
                        idCardReaderModule.open();
                    }
                    if (!idCardReaderModule.isOpen()) {
                        idCardReaderModule.close();
                        if (reading) {
                            emitter.onError(new Throwable("读卡器打开失败"));
                        } else {
                            emitter.onNext(new IDCardInfo());
                        }
                    } else {
                        IDCardReaderRetInfo retInfo = idCardReaderModule.getIDcardInfo();
                        if (retInfo.errCode == IDCardReaderRetInfo.ERROR_OK && retInfo.card != null) {
                            IDCardInfo info = new IDCardInfo();
                            info.setName(retInfo.card.getName());
                            info.setCardNo(retInfo.card.getIDCardNo());
                            info.setSex(retInfo.card.getSex());
                            info.setBirthDay(retInfo.card.getBirthday());
                            info.setAddress(retInfo.card.getAddress());
                            info.setBitmap(retInfo.card.getPhoto());
                            emitter.onNext(info);
                        } else {
                            if (reading) {
                                emitter.onError(new Throwable("未识别到身份证"));
                            } else {
                                emitter.onNext(new IDCardInfo());
                            }
                        }
                    }
                } catch (Exception e) {
                    if (reading) {
                        emitter.onError(new Throwable("没有检测到读卡器"));
                    } else {
                        emitter.onNext(new IDCardInfo());
                    }
                }
            }
        })
                .retryWhen(new RetryWithDelay())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<IDCardInfo>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull IDCardInfo o) {
                        if(o.getName() == null){
                            listener.onSucceed(null);
                        }else {
                            listener.onSucceed(o);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //retryWhen 要在线程切换之前
                        listener.onError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void cancelReadIDCard() {
        reading = false;
    }


}